package com.ztorn.fiscale.controller;

import java.util.List;

import com.ztorn.common.core.web.controller.BaseController;
import com.ztorn.common.core.web.domain.AjaxResult;
import com.ztorn.common.log.annotation.Log;
import com.ztorn.common.log.enums.BusinessType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ztorn.fiscale.domain.FiscaleDocument;
import com.ztorn.fiscale.domain.FiscaleDocumentRelation;
import com.ztorn.fiscale.service.IFiscaleDocumentRelationService;

/**
 * 单证关联Controller
 *
 * @author ztorn
 */
@RestController
@RequestMapping("/documentRelation")
public class FiscaleDocumentRelationController extends BaseController
{
    @Autowired
    private IFiscaleDocumentRelationService fiscaleDocumentRelationService;

    /**
     * 查询单证关联列表
     */
    @PreAuthorize("@ss.hasPermi('fiscale:documentRelation:list')")
    @GetMapping("/list")
    public AjaxResult list(FiscaleDocumentRelation fiscaleDocumentRelation)
    {
        List<FiscaleDocumentRelation> list = fiscaleDocumentRelationService.selectFiscaleDocumentRelationList(fiscaleDocumentRelation);
        return AjaxResult.success(list);
    }

    /**
     * 查询单证的关联单证列表
     */
    @PreAuthorize("@ss.hasPermi('fiscale:documentRelation:query')")
    @GetMapping("/related/{documentId}")
    public AjaxResult getRelatedDocuments(@PathVariable("documentId") Long documentId)
    {
        List<FiscaleDocument> list = fiscaleDocumentRelationService.selectRelatedDocumentList(documentId);
        return AjaxResult.success(list);
    }

    /**
     * 获取单证关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('fiscale:documentRelation:query')")
    @GetMapping(value = "/{relationId}")
    public AjaxResult getInfo(@PathVariable("relationId") Long relationId)
    {
        return AjaxResult.success(fiscaleDocumentRelationService.selectFiscaleDocumentRelationById(relationId));
    }

    /**
     * 新增单证关联
     */
    @PreAuthorize("@ss.hasPermi('fiscale:documentRelation:add')")
    @Log(title = "单证关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FiscaleDocumentRelation fiscaleDocumentRelation)
    {
        return toAjax(fiscaleDocumentRelationService.insertFiscaleDocumentRelation(fiscaleDocumentRelation));
    }

    /**
     * 批量添加单证关联
     */
    @PreAuthorize("@ss.hasPermi('fiscale:documentRelation:add')")
    @Log(title = "单证关联", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd/{documentId}")
    public AjaxResult batchAdd(@PathVariable("documentId") Long documentId, @RequestBody Long[] relatedDocumentIds)
    {
        return toAjax(fiscaleDocumentRelationService.batchAddDocumentRelations(documentId, relatedDocumentIds));
    }

    /**
     * 修改单证关联
     */
    @PreAuthorize("@ss.hasPermi('fiscale:documentRelation:edit')")
    @Log(title = "单证关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FiscaleDocumentRelation fiscaleDocumentRelation)
    {
        return toAjax(fiscaleDocumentRelationService.updateFiscaleDocumentRelation(fiscaleDocumentRelation));
    }

    /**
     * 删除单证关联
     */
    @PreAuthorize("@ss.hasPermi('fiscale:documentRelation:remove')")
    @Log(title = "单证关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{relationIds}")
    public AjaxResult remove(@PathVariable Long[] relationIds)
    {
        return toAjax(fiscaleDocumentRelationService.deleteFiscaleDocumentRelationByIds(relationIds));
    }
}
